const express = require("express");

const common = require('../util/common');

/**
 * @typedef {BookDB}
 */
var BookDB = require('../sqlite/bookdb');

module.exports = {
    find,
    findAll,
    add,
    update,
    remove,
    search,
    findByIsbn    
};

/**
 * 获取指定ID的书籍信息
 * @param {express.Request} req 
 * @param {express.Response} res 
 * @param {express.NextFunction} next 
 */
function find(req, res, next) {
    (async function () {
        let db = BookDB.getInstance();
        await db.connect();
        let result = await db.find(req.params.id);
        await db.close();
        res.json(common.getReturnJSONData(200, `获取指定ID{${req.params.id}}书籍信息`, result));
    })();
}

/**
 * 获取指定范围的书籍信息
 * @param {express.Request} req 
 * @param {express.Response} res 
 * @param {express.NextFunction} next 
 */
function findAll(req, res, next) {
    (async function () {
        let limit = req.query.limit ? req.query.limit : -1;
        let offset = req.query.offset ? req.query.offset : -1;
        common.log(`limit=${limit}, offset=${offset}`);
        let db = BookDB.getInstance();
        await db.connect();
        let result = await db.findAll(limit, offset);
        await db.close();
        res.json(common.getReturnJSONData(200, '获取全部书籍信息', result));
    })();
}

/**
 * 获取指定isbn的书籍信息
 * @param {express.Request} req 
 * @param {express.Response} res 
 * @param {express.NextFunction} next 
 */
function findByIsbn(req, res, next) {
    (async function () {
        let db = BookDB.getInstance();
        await db.connect();
        let result = await db.findByIsbn(req.params.isbn);
        await db.close();
        res.json(common.getReturnJSONData(200, `获取指定ISBN{${req.params.isbn}}书籍信息`, result));

    })();

}

/**
 * 获取指定条件的书籍信息
 * @param {express.Request} req 
 * @param {express.Response} res 
 * @param {express.NextFunction} next 
 */
function search(req, res, next) {
    (async function () {
        let q = req.query.q;
        let limit = req.query.limit;
        let db = BookDB.getInstance();
        await db.connect();
        let result = await db.search(q, limit);
        await db.close();
        res.json(common.getReturnJSONData(200, `获取指定条件书籍信息`, result));
    })();
}

/**
 * 新增书籍信息
 * @param {express.Request} req 
 * @param {express.Response} res 
 * @param {express.NextFunction} next 
 */
function add(req, res, next) {
    (async function () {
        let book = {
            title: req.body.title,
            pic: req.body.pic,
            author: req.body.author,
            publisher: req.body.publisher,
            translator: req.body.translator,
            pubdate: req.body.pubdate,
            pages: req.body.pages,
            price: req.body.price,
            binding: req.body.binding,
            series: req.body.series,
            isbn: req.body.isbn,
            created_time: Date.now(),
            updated_time: Date.now()
        };
        let db = BookDB.getInstance();
        await db.connect();
        let result = await db.add(book);
        await db.close();
        res.json(common.getReturnJSONData(201, '书籍信息添加成功', { id: result }));
    })();

}

/**
 * 更新指定ID的书籍信息
 * @param {express.Request} req 
 * @param {express.Response} res 
 * @param {express.NextFunction} next 
 */
function update(req, res, next) {
    (async function () {
        let book = {
            id: req.params.id,
            title: req.body.title,
            pic: req.body.pic,
            author: req.body.author,
            publisher: req.body.publisher,
            translator: req.body.translator,
            pubdate: req.body.pubdate,
            pages: req.body.pages,
            price: req.body.price,
            binding: req.body.binding,
            series: req.body.series,
            isbn: req.body.isbn,
            updated_time: Date.now()
        };
        let db = BookDB.getInstance();
        await db.connect();
        let result = await db.update(book);
        await db.close();
        res.json(common.getReturnJSONData(200, '书籍信息更新成功', { changes: result }));
    })();
}

/**
 * 删除指定ID的书籍信息
 * @param {express.Request} req 
 * @param {express.Response} res 
 * @param {express.NextFunction} next 
 */
function remove(req, res, next) {
    (async function () {
        let db = BookDB.getInstance();
        await db.connect();
        let result = await db.remove(req.params.id);
        await db.close();
        res.json(common.getReturnJSONData(204, '书籍信息删除成功', { changes: result }));
    })();
}